
#include "Config.h"
unsigned char m1=0x00;
unsigned char m2=0x00;
unsigned char m3=0x00;
//unsigned char beq_long=0x00; //key_long--beq
unsigned char cs_turn=0x00; //
//unsigned char cs_back=0x00;
unsigned char time_5ms=0x00;
unsigned char led_beq=0x00; //0 beq 1 led1 2 led2 3 cha
unsigned char time_cd=250;
unsigned char dis_5s=250;
uchar8_t long_led1=0x00;
uchar8_t long_led2=0x00;

void g_gas(void)
{
	int g=0x0000;
	unsigned int g_o2=0x00;
//	g=g_wADCValue-value.value_zero;
	switch(value.dir)
	{
		case 0:
			g=value.g_wADCValue-value.value_zero;
			if(g>threshold) value.value_gas=(unsigned int)(((unsigned long)g*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=0x0000;
		break;
		case 1:
			g=value.value_zero-value.g_wADCValue;
			if(g>threshold) value.value_gas=(unsigned int)(((unsigned long)g*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=0x0000;
		break;
		case 2:
			if((value.g_wADCValue+threshold)<value.value_zero)
			{
				g_o2=(unsigned int)(((unsigned long)(value.value_zero-value.g_wADCValue)*(unsigned long)value.value_multiple)>>7);
				if(g_o2>=gas0) g_o2=gas0;
				value.value_gas=gas0-g_o2;
			}
			else if(value.g_wADCValue>(threshold+value.value_zero)) value.value_gas=gas0+(unsigned int)(((unsigned long)(value.g_wADCValue-value.value_zero)*(unsigned long)value.value_multiple)>>7);
			else value.value_gas=gas0; //209
		break;
	}
}

unsigned char fun_cd(void)
{
	if(time_cd==0x00)
	{
		time_cd=250;
		return 1;
	}
	else --time_cd;
	if((key_short)||(key_cont)||(key_long)) time_cd=250;
	return 0;
}

uchar8_t fun_key_long(unsigned char m,uchar8_t mn)
{
	switch(m)
	{
		case 00:  //B+
			if(mn==1)
			{
				long_led1=1;
				long_led2=0;
			}
			else if(mn==2)
			{
				long_led1=0;
				long_led2=1;
			}
			else if(mn==3)
			{
				long_led1=1;
				long_led2=1;
			}
		break;
		case 05://28: //B-
			long_led1=0;
			long_led2=0;
		break;
		case 50://50: //B+
			if(mn==1)
			{
				long_led1=1;
				long_led2=0;
			}
			else if(mn==2)
			{
				long_led1=0;
				long_led2=1;
			}
			else if(mn==3)
			{
				long_led1=1;
				long_led2=1;
			}
		break;
		case 55://53: //B-
			long_led1=0;
			long_led2=0;
		break;
		case 100://75: //B+
			if(mn==1)
			{
				long_led1=1;
				long_led2=0;
			}
			else if(mn==2)
			{
				long_led1=0;
				long_led2=1;
			}
			else if(mn==3)
			{
				long_led1=1;
				long_led2=1;
			}
		break;
		case 105://78: //B-
			long_led1=0;
			long_led2=0;
			return 1;
		break;
	}
	return 0;
}

unsigned char fun_countdown(unsigned int c)
{
	static unsigned char time_0se=20;
	time_0se--;
	if(time_0se<4) value.value_sum+=c;
	if(time_0se==00)
	{
		time_0se=20;
		return 1;
	}
	if(state_control&0b00000001)
	{
		turn_10(c);
		if(cs==0x30) point_on=point_on_s;
	}
	else turn_add(time_0se);
	return 0;
}

uchar8_t fun_low(uint16_t (* value_p),uchar8_t low_high) // f_dir 0 address 1 low 2 high
{
	if((key_short)||(key_cont)) cs_add=dis_new-1;
	point_on=point_on_s;
	if((key_short==0b00000100)||(key_cont==0b00000100))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)<value.value_low) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<a0) (* value_p)++;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)<a0) (* value_p)++;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)<value.value_high) (* value_p)++;
			}
		}
	}
	if((key_short==0b00000010)||(key_cont==0b00000010))
	{
		if(low_high)
		{
			if(value.dir==0x02)
			{
				if((* value_p)>0) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>value.value_low) (* value_p)--;
			}
		}
		else
		{
			if(value.dir==0x02)
			{
				if((* value_p)>value.value_high) (* value_p)--;
			}
			else if((value.dir==0x00)||(value.dir==0x01))
			{
				if((* value_p)>0) (* value_p)--;
			}
		}
	}
	if(key_short==0b00000001)
	{
		CLI();
		eeprom_write_word((void *)(0x20+8*low_high), (* value_p));
		SEI();
		if(low_high)
		{
			if(ee_w0&0x02)
			{
				ee_w0&=0b11111101;
				CLI();
				eeprom_write_byte((void *)(0x3e), ee_w0);
				SEI();
			}
		}
		else
		{
			if(ee_w0&0x01)
			{
				ee_w0&=0b11111110;
				CLI();
				eeprom_write_byte((void *)(0x3e), ee_w0);
				SEI();
			}
		}
		return 1;
	}
//	if((key_short)||(key_long)) bf=0x01;
//	else bf^=0x01;
	if(bf) turn_10((* value_p));
	return 0;
}

uchar8_t fun_ma(uint16_t (* value_ma),uchar8_t f_ma)
{
	uchar8_t i_fma=0x00;
	if((key_short)||(key_cont)) cs_add=dis_new-1;
	if((key_short==0b00000100)||(key_cont==0b00000100)) (* value_ma)++;
	if((key_short==0b00000010)||(key_cont==0b00000010)) (* value_ma)--;
	switch(f_ma)
	{
		case 0x00:
			if((* value_ma)==0) (* value_ma)=1;
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
		break;
		case 0x01: //4ma
			if((* value_ma)==value.value_8ma) (* value_ma)=value.value_8ma-1;
			if((* value_ma)==0) (* value_ma)=1;

		break;
		case 0x02: //8
			if((* value_ma)==value.value_12ma) (* value_ma)=value.value_12ma-1;
			if((* value_ma)==value.value_4ma) (* value_ma)=value.value_4ma+1;
		break;
		case 0x03: //12
			if((* value_ma)==value.value_16ma) (* value_ma)=value.value_16ma-1;
			if((* value_ma)==value.value_8ma) (* value_ma)=value.value_8ma+1;
		break;
		case 0x04: //16
			if((* value_ma)==value.value_20ma) (* value_ma)=value.value_20ma-1;
			if((* value_ma)==value.value_12ma) (* value_ma)=value.value_12ma+1;
		break;
		case 0x05: //20
			if((* value_ma)==0x03ff) (* value_ma)=0x03fe;
			if((* value_ma)==value.value_16ma) (* value_ma)=value.value_16ma+1;
		break;
	}
	if(key_short==0b00000001)
	{
		CLI();
		eeprom_write_word((void *)(0x32+2*f_ma), (* value_ma));
		SEI();
		for(i_fma=0;i_fma<6;i_fma++)
		{
			if(i_fma==f_ma)
			{
				if(ee_w0&(1<<(i_fma+2)))
				{
					ee_w0&=(~(1<<(i_fma+2)));
					CLI();
					eeprom_write_byte((void *)(0x3e), ee_w0);
					SEI();
				}
			}
		}
		return 1;
	}
//	if((key_short)||(key_long)) bf=0x01;
//	else bf^=0x01;
	if(bf) turn_10((* value_ma));
	return 0;
}

